﻿using System;
using System.Collections.Generic;
using System.Data.Linq;
using Dogshow.Model;
using DogshowRepository;
using System.Linq;

namespace Dogshow.Linq2SQLRepository
{
    public class IzlozbeRepository : IIzlozbeRepository
    {
        public int Sacuvaj(Izlozba entity)
        {
            using (var model = new DogshowModel())
            {
                Izlozbe db = null;
                if (entity.Id == 0)
                {
                    db = ModelMapper.Map(entity);
                    model.Izlozbes.InsertOnSubmit(db);
                }
                else
                {
                    db = model.Izlozbes.Single(x => x.Id == entity.Id);
                    ModelMapper.Map(entity, db);
                }
                model.SubmitChanges();
                return db.Id;
            }
        }

        public IEnumerable<Izlozba> VratiSve()
        {
            using (var model = new DogshowModel())
            {
                return model.Izlozbes.Select(ModelMapper.Map).ToArray();
            }
        }

        public Izlozba VratiPoId(int id)
        {
            using (var model = new DogshowModel())
            {
                var dlo = new DataLoadOptions();
                dlo.LoadWith<Izlozbe>(x => x.KinoloskaDrustva);
                model.LoadOptions = dlo;
                var izl = model.Izlozbes.SingleOrDefault(x => x.Id == id);
                if (izl == null) return null;
                return ModelMapper.Map(izl);
            }
        }

        public void SacuvajTitule(int idIzlozbe, IEnumerable<int> titule)
        {
            using (var model = new DogshowModel())
            {
                model.ExecuteCommand("DELETE FROM TituleIzlozbe WHERE IdIzlozbe={0}", idIzlozbe);
                model.TituleIzlozbes.InsertAllOnSubmit(titule.Select(x => new TituleIzlozbe(){IdIzlozbe = idIzlozbe, IdTitule = x}));
                model.Submit();
            }
        }

        public IEnumerable<Model.Titula> VratiTituleIzlozbe(int idIzlozbe)
        {
            using (var model = new DogshowModel())
            {
                var dlo = new DataLoadOptions();
                dlo.LoadWith<TituleIzlozbe>(x => x.Titula);
                model.LoadOptions = dlo;
                return model.TituleIzlozbes.Where(x => x.IdIzlozbe == idIzlozbe).Select(x => ModelMapper.Map(x.Titula)).ToArray();
            }
        }

        public IEnumerable<SudijaURingu> VratiSudijePoRingovima(int idIzlozbe)
        {
            using (var model = new DogshowModel())
            {
                var lo = new DataLoadOptions();
                lo.LoadWith<RasporedRingova>(x => x.Kinolozi);
                model.LoadOptions = lo;
                return model.RasporedRingovas.Where(x => x.IdIzlozbe == idIzlozbe)
                    .Select(ModelMapper.Map)
                    .ToArray();
            }
        }

        public void SacuvajSudijuURingu(SudijaURingu sudijaURingu)
        {
            using (var model = new DogshowModel())
            {
                var postojeci = model.RasporedRingovas
                                     .SingleOrDefault(x => x.IdIzlozbe == sudijaURingu.IdIzlozbe && x.IdSudije == sudijaURingu.IdSudije);
                if (postojeci == null)
                {
                    model.RasporedRingovas.InsertOnSubmit(ModelMapper.Map(sudijaURingu));
                } else
                {
                    ModelMapper.Map(sudijaURingu, postojeci);
                }
                model.Submit();
            }
        }

        public void DodajSudijuURingu(SudijaURingu sudijaURingu)
        {
            using (var model = new DogshowModel())
            {
                model.RasporedRingovas.InsertOnSubmit(ModelMapper.Map(sudijaURingu));
                model.SubmitChanges();
            }
        }

        public void AzurirajSudijuURingu(SudijaURingu sudijaURingu)
        {
            using (var model = new DogshowModel())
            {
                var postojeci = model.RasporedRingovas.Single(
                    x => x.IdIzlozbe == sudijaURingu.IdIzlozbe && x.IdSudije == sudijaURingu.IdSudije);
                ModelMapper.Map(sudijaURingu, postojeci);
                model.Submit();
            }
        }

        public void ObrisiSudijuURingu(SudijaURingu sudijaURingu)
        {
            using (var model = new DogshowModel())
            {
                model.ExecuteCommand("DELETE FROM RasporedRingova WHERE IdIzlozbe={0} AND IdSudije={1}",
                                     sudijaURingu.IdIzlozbe, sudijaURingu.IdSudije);
            }
        }

        public IEnumerable<RasaURingu> VratiRasePasaZaSudiju(int idIzlozbe, int idSudije)
        {
            using (var model = new DogshowModel())
            {
                var lo = new DataLoadOptions();
                lo.LoadWith<RasePoRingovima>(x => x.RasePasa);
                model.LoadOptions = lo;
                return model.RasePoRingovimas.Where(x => x.IdIzlozbe == idIzlozbe && x.IdSudije == idSudije)
                    .Select(ModelMapper.Map).ToArray();
            }
        }

        public void DodajRasuSudiji(RasaURingu rasa)
        {
            using (var model = new DogshowModel())
            {
                model.RasePoRingovimas.InsertOnSubmit(ModelMapper.Map(rasa));
                model.Submit();
            }
        }

        public void ObrisiRasuOdSudije(RasaURingu rasa)
        {
            using (var model = new DogshowModel())
            {
                model.ExecuteCommand("DELETE FROM RasePoRingovima WHERE IdIzlozbe={0} AND IdSudije={1}", rasa.IdIzlozbe,
                                     rasa.IdSudije);
            }
        }

        public bool PostojiRasaURingu(RasaURingu rasa)
        {
            using (var model = new DogshowModel())
            {
                return model.RasePoRingovimas.Any(x => x.IdIzlozbe == rasa.IdIzlozbe 
                                                        && x.IdSudije == rasa.IdSudije 
                                                        && x.IdRase == rasa.IdRase);
            }
        }
    }
}